using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using ViewOnCore.HostContract;
using ViewOnCore.HostContract.Settings;
using ViewOnCore.Helper.WPF;
using SettingsAddIn.Properties;

namespace UI.SettingsPanel
{

    // http://blog.kirupa.com/?p=56
    // http://scorbs.com/2006/12/
    public partial class SettingsVisualStyleControl : System.Windows.Controls.UserControl, ISettingPanel
    {

        #region Variables

        private bool _avoidRecursion = false;

        private VisualPack _visualPack;

        #endregion

        #region Constructor

        public SettingsVisualStyleControl()
        {
            InitializeComponent();

            //---- Add the visual packs
            for (int index = 0; index < Host.UIService.VisualPacks.Count; index++)
            {
                string packName = Host.UIService.VisualPacks[index].Name;

                cboOpen.Items.Add(packName);

                if (packName == Host.UIService.CurrentVisualPackName)
                    cboOpen.SelectedIndex = index;
            }

            //---- Color selectors
            btnColorSelectorIcon.SelectedColor = ((SolidColorBrush)Application.Current.Resources["VOColorIcon"]).Color;
            btnColorSelectorText.SelectedColor = ((SolidColorBrush)Application.Current.Resources["VOColorText"]).Color;

            btnColorSelectorIcon.ColorChanged += new ColorChangedHandler(colorSelectorIcon_ColorChanged);
            btnColorSelectorText.ColorChanged += new ColorChangedHandler(colorSelectorText_ColorChanged);

            //---- Add the list of animations
            cboAnimation.Items.Add("<None>");
            cboAnimation.Items.Add("Animation 1");
            cboAnimation.Items.Add("Animation 2");
            cboAnimation.Items.Add("Animation 3");
            cboAnimation.SelectedIndex = 0;

            //---- Gradients
            gpPicker1.GradientChanged += new GradientChangedHandler(gpPicker1_GradientChanged);
            gpPicker2.GradientChanged += new GradientChangedHandler(gpPicker2_GradientChanged);

            //---- Set the current visual pack
            _visualPack = Host.UIService.LoadVisualPack(Host.UIService.CurrentVisualPackName);

            UpdateUIForCurrentVisualPack();

            //----			
            btnApplyNow.Click += new RoutedEventHandler(btnApplyNow_Click);
            btnNew.Click += new RoutedEventHandler(btnNew_Click);
            btnSave.Click += new RoutedEventHandler(btnSave_Click);
            btnDelete.Click += new RoutedEventHandler(btnDelete_Click);
            cboOpen.SelectionChanged += new SelectionChangedEventHandler(cboOpen_SelectionChanged);
            cssSelector.ColorSelectionChanged += new ColorSchemeSelectorEventHandler(cssSelector_ColorSelectionChanged);

            chkUseImageMode.Checked += new RoutedEventHandler(chkUseImageMode_Checked);
            chkUseImageMode.Unchecked += new RoutedEventHandler(chkUseImageMode_Unchecked);
            chkAdvancedMode.Checked += new RoutedEventHandler(chkAdvancedMode_Checked);
            chkAdvancedMode.Unchecked += new RoutedEventHandler(chkAdvancedMode_Unchecked);
            btnBrowseImage.Click += new RoutedEventHandler(btnBrowseImage_Click);
            cboAnimation.SelectionChanged += new SelectionChangedEventHandler(cboAnimation_SelectionChanged);
            txtXAML.TextChanged += new TextChangedEventHandler(txtXAML_TextChanged);
        }

        #endregion

        #region Properties

        public string Title
        {
            get
            {
                return SettingsAddIn.Properties.Globalization.SettingsPart_VisualStyle;
            }
        }

        #endregion

        #region Normal mode : Image + animation

        void btnBrowseImage_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Forms.OpenFileDialog dialog = new System.Windows.Forms.OpenFileDialog();
            dialog.Filter = Globalization.SettingsVisualStyleControl_ImageFiles + "|*.gif;*.jpg;*.jpeg;*.png;*.bmp|All files (*.*)|*.*";
            dialog.Title = Globalization.SettingsVisualStyleControl_SelectImageFile;
            dialog.Multiselect = false;
            dialog.CheckFileExists = true;

            if (dialog.ShowDialog() != System.Windows.Forms.DialogResult.OK)
                return;

            txtImagePath.Text = dialog.FileName;

            _visualPack.ImagePath = txtImagePath.Text;

            UpdateXAML();
        }

        void chkUseImageAnimation_Checked(object sender, RoutedEventArgs e)
        {
            UpdateXAML();
        }

        void cboAnimation_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            _visualPack.Animation = cboAnimation.SelectedValue.ToString();

            UpdateXAML();
        }

        public void UpdateXAML()
        {
            txtXAML.Text = GenerateXAML();
        }

        #endregion

        #region UpdateUIForCurrentVisualPack

        void UpdateUIForCurrentVisualPack()
        {
            //---- Reset
            if (_visualPack.Mode == VisualPackMode.Normal)
            {
                chkUseImageMode.IsChecked = true;
                chkAdvancedMode.IsChecked = false;
                SetNormalMode();
            }
            else
            {
                chkUseImageMode.IsChecked = false;
                chkAdvancedMode.IsChecked = true;
                SetAdvancedMode();
            }

            if (!_visualPack.CanModify)
                btnDelete.IsEnabled = false;
            else
                btnDelete.IsEnabled = true;

            cboAnimation.SelectedValue = _visualPack.Animation;
            cssSelector.StartColor = _visualPack.Color10;
            cssSelector.EndColor = _visualPack.Color1A;
            recColorText.Fill = new SolidColorBrush(_visualPack.ColorText);
            recColorIcon.Fill = new SolidColorBrush(_visualPack.ColorIcon);
            txtImagePath.Text = _visualPack.ImagePath;
            txtXAML.Text = _visualPack.XAML;

            gpPicker1.SetBrush(_visualPack.Gradient01);
            gpPicker2.SetBrush(_visualPack.Gradient02);
        }

        #endregion

        #region Button : apply

        void btnApplyNow_Click(object sender, RoutedEventArgs e)
        {
            Apply();
        }

        #endregion

        #region Button : save as

        void btnSave_Click(object sender, RoutedEventArgs e)
        {
            SaveAs(false);
        }

        #endregion

        #region Button : new

        void btnNew_Click(object sender, RoutedEventArgs e)
        {
            _visualPack = Host.UIService.NewVisualPack();

            SetNormalMode();

            //---- Reset
            UpdateUIForCurrentVisualPack();
        }

        #endregion

        #region Button : delete

        void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            cboOpen.SelectionChanged -= new SelectionChangedEventHandler(cboOpen_SelectionChanged);

            Host.UIService.DeleteVisualPack(cboOpen.SelectedItem.ToString());
            cboOpen.Items.Remove(cboOpen.SelectedItem);
            cboOpen.SelectedValue = "Default";

            cboOpen.SelectionChanged += new SelectionChangedEventHandler(cboOpen_SelectionChanged);

            cboOpen_SelectionChanged(null, null);
        }

        #endregion

        #region Combo : Open

        void cboOpen_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            _visualPack = Host.UIService.FindVisualPack(cboOpen.SelectedValue.ToString());
            if (_visualPack == null)
                _visualPack = Host.UIService.LoadVisualPack(cboOpen.SelectedValue.ToString());

            UpdateUIForCurrentVisualPack();
        }

        #endregion

        #region Color scheme : color selection changed

        void cssSelector_ColorSelectionChanged()
        {
            _visualPack.Color10 = cssSelector.Color10;
            _visualPack.Color11 = cssSelector.Color11;
            _visualPack.Color12 = cssSelector.Color12;
            _visualPack.Color13 = cssSelector.Color13;
            _visualPack.Color14 = cssSelector.Color14;
            _visualPack.Color15 = cssSelector.Color15;
            _visualPack.Color16 = cssSelector.Color16;
            _visualPack.Color17 = cssSelector.Color17;
            _visualPack.Color18 = cssSelector.Color18;
            _visualPack.Color19 = cssSelector.Color19;
            _visualPack.Color1A = cssSelector.Color1A;
        }

        #endregion

        #region Color selector : text & icon : events

        void colorSelectorText_ColorChanged(Color color)
        {
            _visualPack.ColorText = color;
            recColorText.Fill = new SolidColorBrush(color);
        }

        void colorSelectorIcon_ColorChanged(Color color)
        {
            _visualPack.ColorIcon = color;
            recColorIcon.Fill = new SolidColorBrush(color);
        }

        #endregion

        #region Color gradient

        void gpPicker1_GradientChanged(LinearGradientBrush brush)
        {
            _visualPack.Gradient01 = brush;
        }

        void gpPicker2_GradientChanged(LinearGradientBrush brush)
        {
            _visualPack.Gradient02 = brush;
        }

        #endregion

        #region Text : XAML text changed

        void txtXAML_TextChanged(object sender, TextChangedEventArgs e)
        {
            _visualPack.XAML = txtXAML.Text;
        }

        #endregion

        #region Save...

        private void Save()
        {
            //---- Save as
            if (_visualPack.Name == null || _visualPack.Name.Trim().Length < 1)
            {
                SaveAs(false);
                return;
            }

            //---- Save
            Host.UIService.SaveVisualPack(_visualPack);
        }

        private void SaveAs(bool apply)
        {
            string message = "Enter the Visual Pack's name :";
            string message2 = "";
            string initialValue = _visualPack.Name;
            if (initialValue == null)
                initialValue = "";

            initialValue = initialValue.Trim();

            QuestionDialog dialog = new QuestionDialog(QuestionDialog.QuestionDialogType.SaveCancel, message, message2, initialValue, null, 350);
            dialog.Tag = apply;
            dialog.ShowDialog(Host.ApplicationDesktop,
                                                new ChildWindowDialogClosedHandler(OnConfirmSave));
        }

        private void OnConfirmSave(ChildWindowDecorator window)
        {
            //---- Check if it is Ok to delete
            QuestionDialog dialog = window.Parent as QuestionDialog;

            if (!dialog.IsOkYes)
                return;

            bool apply = (bool)dialog.Tag;

            //---- Bad name
            if (dialog.Value.Trim().Length < 1)
            {
                MessageDialog messageDialog = new MessageDialog(Globalization.SettingsVisualStyleControl_InvalidName, "", 350);
                messageDialog.ShowDialog((Desktop)Host.ApplicationDesktop);
                return;
            }

            //---- Copy the visual pack
            if (_visualPack.Name != dialog.Value)
            {
                VisualPack newPack = Host.UIService.NewVisualPack();
                _visualPack.CopyTo(newPack);
                newPack.Name = dialog.Value.Trim();

                _visualPack = newPack;
                Host.UIService.CurrentVisualPackName = _visualPack.Name;
            }

            //---- Save the visual pack
            Host.UIService.SaveVisualPack(_visualPack);

            //---- Add the visual pack if it has a new name
            if (!cboOpen.Items.Contains(dialog.Value))
                cboOpen.Items.Add(dialog.Value);

            // Set the new name
            cboOpen.SelectionChanged -= new SelectionChangedEventHandler(cboOpen_SelectionChanged);
            cboOpen.SelectedValue = _visualPack.Name;
            cboOpen.SelectionChanged += new SelectionChangedEventHandler(cboOpen_SelectionChanged);

            //---- Apply
            if (apply)
                Apply();
        }

        #endregion

        #region Check box : change mode

        void chkAdvancedMode_Checked(object sender, RoutedEventArgs e)
        {
            if (_avoidRecursion)
                return;
            _avoidRecursion = true;

            SetAdvancedMode();
            chkUseImageMode.IsChecked = false;

            _avoidRecursion = false;
        }

        void chkUseImageMode_Checked(object sender, RoutedEventArgs e)
        {
            if (_avoidRecursion)
                return;
            _avoidRecursion = true;

            SetNormalMode();
            chkAdvancedMode.IsChecked = false;

            _avoidRecursion = false;
        }

        void chkAdvancedMode_Unchecked(object sender, RoutedEventArgs e)
        {
            if (_avoidRecursion)
                return;
            chkAdvancedMode.IsChecked = true;
        }

        void chkUseImageMode_Unchecked(object sender, RoutedEventArgs e)
        {
            if (_avoidRecursion)
                return;
            chkUseImageMode.IsChecked = true;
        }

        #endregion

        #region Set mode

        public void SetNormalMode()
        {
            btnBrowseImage.IsEnabled = true;
            cboAnimation.IsEnabled = true;

            txtXAML.IsEnabled = false;

            _visualPack.Mode = VisualPackMode.Normal;
        }

        public void SetAdvancedMode()
        {
            btnBrowseImage.IsEnabled = false;
            cboAnimation.IsEnabled = false;

            txtXAML.IsEnabled = true;

            _visualPack.Mode = VisualPackMode.Advanced;
        }

        #endregion

        #region Apply

        public void Apply()
        {
            if (_visualPack.CanModify)
            {
                //---- Save before !
                if (_visualPack.Name == null || _visualPack.Name.Trim().Length < 1)
                {
                    SaveAs(true);
                    return;
                }

                //---- Save
                Save();
            }

            //---- Set new one as current
            string errorMessage = Host.UIService.ApplyVisualPack(_visualPack);
            if (errorMessage != null)
            {
                MessageDialog dialog = new MessageDialog("An error occured", errorMessage, 350);
                dialog.ShowDialog((Desktop)Host.ApplicationDesktop);
            }
        }

        #endregion

        #region Cancel

        public void Cancel()
        {
            //---- Restore previous visual pack
            _visualPack = Host.UIService.LoadVisualPack(Host.UIService.CurrentVisualPackName);

            //---- Apply changes
            //string errorMessage = Host.UIService.ApplyVisualPack(_visualPack);
            //if (errorMessage != null)
            //{
            //    MessageDialog dialog = new MessageDialog(Globalization.SettingsVisualStyleControl_ErrorOccured, errorMessage, 350);
            //    dialog.ShowDialog(Host.ApplicationDesktop);
            //}
        }

        #endregion

        #region OnDisplay

        public void Reset()
        {
            Cancel();
        }

        #endregion

        #region Generate XAML

        private string GenerateXAML()
        {
            //---- Animation
            switch (cboAnimation.SelectedIndex)
            {
                case 0:
                    return @"<Image Source=""" + txtImagePath.Text + @""" Stretch=""UniformToFill""/>";
                case 1:
                    return GenerateXAMLWithAnimation_Animated01();
                case 2:
                    return GenerateXAMLWithAnimation_Animation02();
                case 3:
                    return GenerateXAMLWithAnimation_Animation03();
            }

            return "";
        }

        #endregion

        #region GenerateXAMLWithAnimation_Animated01

        private string GenerateXAMLWithAnimation_Animated01()
        {
            string xaml = @"<Image Grid.RowSpan=""2"" Stretch=""UniformToFill"" Source=""" + txtImagePath.Text + @""">" + Environment.NewLine;
            xaml += @"<Image.RenderTransform>" + Environment.NewLine;
            xaml += @"<TransformGroup>" + Environment.NewLine;
            xaml += @"<ScaleTransform ScaleX=""1"" ScaleY=""1"" CenterX=""240"" CenterY=""250"" />" + Environment.NewLine;
            xaml += @"<SkewTransform AngleX=""0"" AngleY=""0"" />" + Environment.NewLine;
            xaml += @"<RotateTransform Angle=""0"" />" + Environment.NewLine;
            xaml += @"<TranslateTransform X=""0"" Y=""0"" />" + Environment.NewLine;
            xaml += @"</TransformGroup>" + Environment.NewLine;
            xaml += @"</Image.RenderTransform>" + Environment.NewLine;
            xaml += @"<Image.Resources>" + Environment.NewLine;
            xaml += @"<Storyboard x:Key=""Loaded"" SpeedRatio="".01"" RepeatBehavior=""Forever"" BeginTime=""0:0:5"" AutoReverse=""true"" DesiredFrameRate=""10"">" + Environment.NewLine;
            xaml += @"<DoubleAnimationUsingKeyFrames BeginTime=""00:00:00"" Storyboard.TargetProperty=""(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)"">" + Environment.NewLine;
            xaml += @"<SplineDoubleKeyFrame KeyTime=""00:00:00"" Value=""1"" />" + Environment.NewLine;
            xaml += @"<SplineDoubleKeyFrame KeyTime=""00:00:1.5"" Value=""3"" />" + Environment.NewLine;
            xaml += @"<SplineDoubleKeyFrame KeyTime=""00:00:3"" Value=""1"" />" + Environment.NewLine;
            xaml += @"</DoubleAnimationUsingKeyFrames>" + Environment.NewLine;
            xaml += @"<DoubleAnimationUsingKeyFrames BeginTime=""00:00:00"" Storyboard.TargetProperty=""(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)"">" + Environment.NewLine;
            xaml += @"<SplineDoubleKeyFrame KeyTime=""00:00:00"" Value=""1"" />" + Environment.NewLine;
            xaml += @"<SplineDoubleKeyFrame KeyTime=""00:00:1.5"" Value=""3"" />" + Environment.NewLine;
            xaml += @"<SplineDoubleKeyFrame KeyTime=""00:00:3"" Value=""1"" />" + Environment.NewLine;
            xaml += @"</DoubleAnimationUsingKeyFrames>" + Environment.NewLine;
            xaml += @"</Storyboard>" + Environment.NewLine;
            xaml += @"</Image.Resources>" + Environment.NewLine;
            xaml += @"<Image.Triggers>" + Environment.NewLine;
            xaml += @"<EventTrigger RoutedEvent=""FrameworkElement.Loaded"">" + Environment.NewLine;
            xaml += @"<BeginStoryboard Storyboard=""{StaticResource Loaded}"" />" + Environment.NewLine;
            xaml += @"</EventTrigger>" + Environment.NewLine;
            xaml += @"</Image.Triggers>" + Environment.NewLine;
            xaml += @"</Image>" + Environment.NewLine;

            return xaml;
        }

        #endregion

        #region GenerateXAMLWithAnimation_Animation02

        private string GenerateXAMLWithAnimation_Animation02()
        {
            string xaml = @"<Viewbox Stretch=""Fill"">" + Environment.NewLine;

            xaml += @"<Viewbox.Resources>" + Environment.NewLine;
            xaml += @"<Style x:Key =""GradientPanel"" TargetType =""{x:Type Panel}"">" + Environment.NewLine;
            xaml += @"<Setter Property =""Background"">" + Environment.NewLine;
            xaml += @"<Setter.Value>" + Environment.NewLine;
            xaml += @"<DrawingBrush Stretch =""UniformToFill"">" + Environment.NewLine;
            xaml += @"<DrawingBrush.Drawing>" + Environment.NewLine;
            xaml += @"<DrawingGroup>" + Environment.NewLine;
            xaml += @"<GeometryDrawing>" + Environment.NewLine;
            xaml += @"<GeometryDrawing.Brush>" + Environment.NewLine;
            xaml += @"<SolidColorBrush Color =""White""/>" + Environment.NewLine;
            xaml += @"</GeometryDrawing.Brush>" + Environment.NewLine;
            xaml += @"<GeometryDrawing.Geometry>" + Environment.NewLine;
            xaml += @"<RectangleGeometry Rect=""0,0,1,1""/>" + Environment.NewLine;
            xaml += @"</GeometryDrawing.Geometry>" + Environment.NewLine;
            xaml += @"</GeometryDrawing>" + Environment.NewLine;
            xaml += @"<GeometryDrawing>" + Environment.NewLine;
            xaml += @"<GeometryDrawing.Brush>" + Environment.NewLine;
            xaml += @"<RadialGradientBrush>" + Environment.NewLine;
            xaml += @"<RadialGradientBrush.RelativeTransform>" + Environment.NewLine;
            xaml += @"<TranslateTransform X =""-0.2"" Y =""0""/>" + Environment.NewLine;
            xaml += @"</RadialGradientBrush.RelativeTransform>" + Environment.NewLine;
            xaml += @"<GradientStop Color=""#850039d6"" Offset =""0""/>" + Environment.NewLine;
            xaml += @"<GradientStop Color=""#654577Ff"" Offset =""0""/>" + Environment.NewLine;
            xaml += @"<GradientStop Color=""#850039d6"" Offset =""0.33""/>" + Environment.NewLine;
            xaml += @"<GradientStop Color=""#654577FF"" Offset =""0.66""/>" + Environment.NewLine;
            xaml += @"<GradientStop Color=""#850039D6"" Offset =""1""/>" + Environment.NewLine;
            xaml += @"</RadialGradientBrush>" + Environment.NewLine;
            xaml += @"</GeometryDrawing.Brush>" + Environment.NewLine;
            xaml += @"<GeometryDrawing.Geometry>" + Environment.NewLine;
            xaml += @"<RectangleGeometry Rect=""0,0,1,1""/>" + Environment.NewLine;
            xaml += @"</GeometryDrawing.Geometry>" + Environment.NewLine;
            xaml += @"</GeometryDrawing>" + Environment.NewLine;
            xaml += @"<GeometryDrawing>" + Environment.NewLine;
            xaml += @"<GeometryDrawing.Brush>" + Environment.NewLine;
            xaml += @"<RadialGradientBrush>" + Environment.NewLine;
            xaml += @"<RadialGradientBrush.RelativeTransform>" + Environment.NewLine;
            xaml += @"<TransformGroup>" + Environment.NewLine;
            xaml += @"<ScaleTransform CenterX =""0.5"" CenterY =""0.5"" ScaleX =""0.85"" ScaleY =""0.85""/>" + Environment.NewLine;
            xaml += @"<TranslateTransform X =""0.35"" Y =""-0.35""/>" + Environment.NewLine;
            xaml += @"</TransformGroup>" + Environment.NewLine;
            xaml += @"</RadialGradientBrush.RelativeTransform>" + Environment.NewLine;
            xaml += @"<GradientStop Color=""#650039d6"" Offset =""0""/>" + Environment.NewLine;
            xaml += @"<GradientStop Color=""#354577Ff"" Offset =""0""/>" + Environment.NewLine;
            xaml += @"<GradientStop Color=""#650039d6"" Offset =""0.33""/>" + Environment.NewLine;
            xaml += @"<GradientStop Color=""#354577FF"" Offset =""0.66""/>" + Environment.NewLine;
            xaml += @"<GradientStop Color=""#650039D6"" Offset =""0.9""/>" + Environment.NewLine;
            xaml += @"</RadialGradientBrush>" + Environment.NewLine;
            xaml += @"</GeometryDrawing.Brush>" + Environment.NewLine;
            xaml += @"<GeometryDrawing.Geometry>" + Environment.NewLine;
            xaml += @"<RectangleGeometry Rect=""0,0,1,1""/>" + Environment.NewLine;
            xaml += @"</GeometryDrawing.Geometry>" + Environment.NewLine;
            xaml += @"</GeometryDrawing>" + Environment.NewLine;
            xaml += @"<GeometryDrawing>" + Environment.NewLine;
            xaml += @"<GeometryDrawing.Brush>" + Environment.NewLine;
            xaml += @"<RadialGradientBrush>" + Environment.NewLine;
            xaml += @"<RadialGradientBrush.RelativeTransform>" + Environment.NewLine;
            xaml += @"<TransformGroup>" + Environment.NewLine;
            xaml += @"<ScaleTransform CenterX =""0.5"" CenterY =""0.5"" ScaleX =""0.75"" ScaleY =""0.75""/>" + Environment.NewLine;
            xaml += @"<TranslateTransform X =""0.25"" Y =""0.35""/>" + Environment.NewLine;
            xaml += @"</TransformGroup>" + Environment.NewLine;
            xaml += @"</RadialGradientBrush.RelativeTransform>" + Environment.NewLine;
            xaml += @"<GradientStop Color=""#650039d6"" Offset =""0""/>" + Environment.NewLine;
            xaml += @"<GradientStop Color=""#354577Ff"" Offset =""0""/>" + Environment.NewLine;
            xaml += @"<GradientStop Color=""#650039d6"" Offset =""0.33""/>" + Environment.NewLine;
            xaml += @"<GradientStop Color=""#004577FF"" Offset =""0.66""/>" + Environment.NewLine;
            xaml += @"<GradientStop Color=""#000039D6"" Offset =""0.9""/>" + Environment.NewLine;
            xaml += @"</RadialGradientBrush>" + Environment.NewLine;
            xaml += @"</GeometryDrawing.Brush>" + Environment.NewLine;
            xaml += @"<GeometryDrawing.Geometry>" + Environment.NewLine;
            xaml += @"<RectangleGeometry Rect=""0,0,1,1""/>" + Environment.NewLine;
            xaml += @"</GeometryDrawing.Geometry>" + Environment.NewLine;
            xaml += @"</GeometryDrawing>" + Environment.NewLine;
            xaml += @"<GeometryDrawing>" + Environment.NewLine;
            xaml += @"<GeometryDrawing.Brush>" + Environment.NewLine;
            xaml += @"<RadialGradientBrush>" + Environment.NewLine;
            xaml += @"<RadialGradientBrush.RelativeTransform>" + Environment.NewLine;
            xaml += @"<TranslateTransform X =""-0.1"" Y =""0.05""/>" + Environment.NewLine;
            xaml += @"</RadialGradientBrush.RelativeTransform>" + Environment.NewLine;
            xaml += @"<GradientStop Color=""#450031B7"" Offset =""0""/>" + Environment.NewLine;
            xaml += @"<GradientStop Color=""#251B57F7"" Offset =""0.25""/>" + Environment.NewLine;
            xaml += @"<GradientStop Color=""#350039D6"" Offset =""0.65""/>" + Environment.NewLine;
            xaml += @"<GradientStop Color=""#154577FF"" Offset =""0.85""/>" + Environment.NewLine;
            xaml += @"<GradientStop Color =""Transparent"" Offset =""1""/>" + Environment.NewLine;
            xaml += @"</RadialGradientBrush>" + Environment.NewLine;
            xaml += @"</GeometryDrawing.Brush>" + Environment.NewLine;
            xaml += @"<GeometryDrawing.Geometry>" + Environment.NewLine;
            xaml += @"<RectangleGeometry Rect=""0,0,1,1""/>" + Environment.NewLine;
            xaml += @"</GeometryDrawing.Geometry>" + Environment.NewLine;
            xaml += @"</GeometryDrawing>" + Environment.NewLine;
            xaml += @"<GeometryDrawing>" + Environment.NewLine;
            xaml += @"<GeometryDrawing.Brush>" + Environment.NewLine;
            xaml += @"<RadialGradientBrush>" + Environment.NewLine;
            xaml += @"<RadialGradientBrush.RelativeTransform>" + Environment.NewLine;
            xaml += @"<TransformGroup>" + Environment.NewLine;
            xaml += @"<ScaleTransform CenterX =""0.5"" CenterY =""0.5"" ScaleX =""1.5"" ScaleY =""1.5""/>" + Environment.NewLine;
            xaml += @"<TranslateTransform X =""-0.025"" Y =""0.06""/>" + Environment.NewLine;
            xaml += @"</TransformGroup>" + Environment.NewLine;
            xaml += @"</RadialGradientBrush.RelativeTransform>" + Environment.NewLine;
            xaml += @"<GradientStop Color=""#351B57F7"" Offset =""0""/>" + Environment.NewLine;
            xaml += @"<GradientStop Color=""#250031B7"" Offset =""0.5""/>" + Environment.NewLine;
            xaml += @"<GradientStop Color=""#354577FF"" Offset =""0.75""/>" + Environment.NewLine;
            xaml += @"<GradientStop Color =""Transparent"" Offset =""0.9""/>" + Environment.NewLine;
            xaml += @"<GradientStop Color=""#350039D6"" Offset =""1""/>" + Environment.NewLine;
            xaml += @"</RadialGradientBrush>" + Environment.NewLine;
            xaml += @"</GeometryDrawing.Brush>" + Environment.NewLine;
            xaml += @"<GeometryDrawing.Geometry>" + Environment.NewLine;
            xaml += @"<RectangleGeometry Rect=""0,0,1,1""/>" + Environment.NewLine;
            xaml += @"</GeometryDrawing.Geometry>" + Environment.NewLine;
            xaml += @"</GeometryDrawing>" + Environment.NewLine;
            xaml += @"</DrawingGroup>" + Environment.NewLine;
            xaml += @"</DrawingBrush.Drawing>" + Environment.NewLine;
            xaml += @"</DrawingBrush>" + Environment.NewLine;
            xaml += @"</Setter.Value>" + Environment.NewLine;
            xaml += @"</Setter>" + Environment.NewLine;
            xaml += @"<Style.Triggers>" + Environment.NewLine;
            xaml += @"<EventTrigger RoutedEvent =""Control.Loaded"">" + Environment.NewLine;
            xaml += @"<EventTrigger.Actions>" + Environment.NewLine;
            xaml += @"<BeginStoryboard>" + Environment.NewLine;
            xaml += @"<Storyboard >" + Environment.NewLine;
            xaml += @"<ColorAnimation RepeatBehavior=""Forever"" Duration =""0:0:4"" Storyboard.TargetProperty =""Background.Drawing.Children[1].Brush.GradientStops[0].Color"" From =""#654577Ff""/>" + Environment.NewLine;
            xaml += @"<DoubleAnimation RepeatBehavior=""Forever"" Duration=""0:0:4"" Storyboard.TargetProperty =""Background.Drawing.Children[1].Brush.GradientStops[0].Offset"" From =""0"" To =""0.33""/>" + Environment.NewLine;
            xaml += @"<DoubleAnimation  RepeatBehavior=""Forever"" Duration=""0:0:4"" Storyboard.TargetProperty =""Background.Drawing.Children[1].Brush.GradientStops[1].Offset"" From =""0"" To =""0.66""/>" + Environment.NewLine;
            xaml += @"<DoubleAnimation RepeatBehavior=""Forever"" Duration=""0:0:4"" Storyboard.TargetProperty =""Background.Drawing.Children[1].Brush.GradientStops[2].Offset"" From =""0.33"" To =""1""/>" + Environment.NewLine;
            xaml += @"<DoubleAnimation RepeatBehavior=""Forever"" Duration=""0:0:4"" Storyboard.TargetProperty =""Background.Drawing.Children[1].Brush.GradientStops[3].Offset"" From =""0.66"" To =""1.33""/>" + Environment.NewLine;
            xaml += @"<DoubleAnimation RepeatBehavior=""Forever"" Duration=""0:0:4"" Storyboard.TargetProperty =""Background.Drawing.Children[1].Brush.GradientStops[4].Offset"" From =""1"" To =""1.66""/>" + Environment.NewLine;
            xaml += @"<ColorAnimation RepeatBehavior=""Forever"" Duration =""0:0:2"" Storyboard.TargetProperty =""Background.Drawing.Children[2].Brush.GradientStops[0].Color"" From =""#654577Ff""/>" + Environment.NewLine;
            xaml += @"<DoubleAnimation RepeatBehavior=""Forever"" Duration=""0:0:2"" Storyboard.TargetProperty =""Background.Drawing.Children[2].Brush.GradientStops[0].Offset"" From =""0"" To =""0.33""/>" + Environment.NewLine;
            xaml += @"<DoubleAnimation  RepeatBehavior=""Forever"" Duration=""0:0:2"" Storyboard.TargetProperty =""Background.Drawing.Children[2].Brush.GradientStops[1].Offset"" From =""0"" To =""0.66""/>" + Environment.NewLine;
            xaml += @"<DoubleAnimation RepeatBehavior=""Forever"" Duration=""0:0:2"" Storyboard.TargetProperty =""Background.Drawing.Children[2].Brush.GradientStops[2].Offset"" From =""0.33"" To =""1""/>" + Environment.NewLine;
            xaml += @"<DoubleAnimation RepeatBehavior=""Forever"" Duration=""0:0:2"" Storyboard.TargetProperty =""Background.Drawing.Children[2].Brush.GradientStops[3].Offset"" From =""0.66"" To =""1.33""/>" + Environment.NewLine;
            xaml += @"<DoubleAnimation RepeatBehavior=""Forever"" Duration=""0:0:2"" Storyboard.TargetProperty =""Background.Drawing.Children[2].Brush.GradientStops[4].Offset"" From =""1"" To =""1.66""/>" + Environment.NewLine;
            xaml += @"</Storyboard>" + Environment.NewLine;
            xaml += @"</BeginStoryboard>" + Environment.NewLine;
            xaml += @"</EventTrigger.Actions>" + Environment.NewLine;
            xaml += @"</EventTrigger>" + Environment.NewLine;
            xaml += @"</Style.Triggers>" + Environment.NewLine;
            xaml += @"</Style>" + Environment.NewLine;
            xaml += @"</Viewbox.Resources>" + Environment.NewLine;

            xaml += @"<DockPanel Style =""{StaticResource GradientPanel}"">" + Environment.NewLine;
            xaml += @"<Grid DockPanel.Dock =""Top"" Margin=""10,0,0,25"">" + Environment.NewLine;
            xaml += @"<Grid.RenderTransform>" + Environment.NewLine;
            xaml += @"<TranslateTransform X=""800""/>" + Environment.NewLine;
            xaml += @"</Grid.RenderTransform>" + Environment.NewLine;
            xaml += @"<Grid.Triggers>" + Environment.NewLine;
            xaml += @"<EventTrigger RoutedEvent =""Control.Loaded"">" + Environment.NewLine;
            xaml += @"<EventTrigger.Actions>" + Environment.NewLine;
            xaml += @"<BeginStoryboard>" + Environment.NewLine;
            xaml += @"<Storyboard>" + Environment.NewLine;
            xaml += @"<DoubleAnimationUsingKeyFrames  BeginTime =""0:0:0.25"" Storyboard.TargetProperty=""RenderTransform.X""  AccelerationRatio=""0.25"">" + Environment.NewLine;
            xaml += @"<LinearDoubleKeyFrame Value=""800"" KeyTime=""0:0:0""/>" + Environment.NewLine;
            xaml += @"<LinearDoubleKeyFrame Value=""-30"" KeyTime=""0:0:0.3""/>" + Environment.NewLine;
            xaml += @"<LinearDoubleKeyFrame Value=""20"" KeyTime=""0:0:0.5""/>" + Environment.NewLine;
            xaml += @"<LinearDoubleKeyFrame Value=""0"" KeyTime=""0:0:0.6""/>" + Environment.NewLine;
            xaml += @"</DoubleAnimationUsingKeyFrames>" + Environment.NewLine;
            xaml += @"</Storyboard>" + Environment.NewLine;
            xaml += @"</BeginStoryboard>" + Environment.NewLine;
            xaml += @"</EventTrigger.Actions>" + Environment.NewLine;
            xaml += @"</EventTrigger>" + Environment.NewLine;
            xaml += @"</Grid.Triggers>" + Environment.NewLine;
            xaml += @"<Rectangle VerticalAlignment =""Top"" HorizontalAlignment =""Left"" x:Name =""TitleReflect""  Width =""{Binding ElementName=TitleText,Path=ActualWidth}"" Height =""{Binding ElementName=TitleText,Path=ActualHeight}"">" + Environment.NewLine;
            xaml += @"<Rectangle.RenderTransform>" + Environment.NewLine;
            xaml += @"<TransformGroup>" + Environment.NewLine;
            xaml += @"<ScaleTransform CenterY=""{Binding ElementName=TitleText,Path=ActualHeight}"" ScaleY =""-1""/>" + Environment.NewLine;
            xaml += @"<TranslateTransform Y =""-10""/>" + Environment.NewLine;
            xaml += @"</TransformGroup>" + Environment.NewLine;
            xaml += @"</Rectangle.RenderTransform>" + Environment.NewLine;
            xaml += @"<Rectangle.OpacityMask>" + Environment.NewLine;
            xaml += @"<LinearGradientBrush StartPoint =""0,0"" EndPoint =""0,1"">" + Environment.NewLine;
            xaml += @"<GradientStop Color =""#00112233"" Offset =""0.3""/>" + Environment.NewLine;
            xaml += @"<GradientStop Color =""#a0112233"" Offset =""1""/>" + Environment.NewLine;
            xaml += @"</LinearGradientBrush>" + Environment.NewLine;
            xaml += @"</Rectangle.OpacityMask>" + Environment.NewLine;
            xaml += @"</Rectangle>" + Environment.NewLine;
            xaml += @"</Grid>" + Environment.NewLine;
            xaml += @"</DockPanel>" + Environment.NewLine;
            xaml += @"</Viewbox>";

            return xaml;
        }

        #endregion

        #region GenerateXAMLWithAnimation_Animated03

        private string GenerateXAMLWithAnimation_Animation03()
        {
            string xaml = @"<Image Grid.RowSpan=""2"" Stretch=""UniformToFill"" Source=""" + txtImagePath.Text + @""">" + Environment.NewLine;
            xaml += @"<Image.RenderTransform>" + Environment.NewLine;
            xaml += @"<TransformGroup>" + Environment.NewLine;
            xaml += @"<ScaleTransform ScaleX=""1"" ScaleY=""1"" CenterX=""240"" CenterY=""250"" />" + Environment.NewLine;
            xaml += @"<SkewTransform AngleX=""0"" AngleY=""0"" />" + Environment.NewLine;
            xaml += @"<RotateTransform Angle=""0"" />" + Environment.NewLine;
            xaml += @"<TranslateTransform X=""0"" Y=""0"" />" + Environment.NewLine;
            xaml += @"</TransformGroup>" + Environment.NewLine;
            xaml += @"</Image.RenderTransform>" + Environment.NewLine;
            xaml += @"<Image.Resources>" + Environment.NewLine;
            xaml += @"<Storyboard x:Key=""Loaded"" SpeedRatio="".01"" RepeatBehavior=""Forever"" BeginTime=""0:0:5"" AutoReverse=""true"" DesiredFrameRate=""10"">" + Environment.NewLine;
            xaml += @"<DoubleAnimationUsingKeyFrames BeginTime=""00:00:00"" Storyboard.TargetProperty=""(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)"">" + Environment.NewLine;
            xaml += @"<SplineDoubleKeyFrame KeyTime=""00:00:00"" Value=""1"" />" + Environment.NewLine;
            xaml += @"<SplineDoubleKeyFrame KeyTime=""00:00:1.5"" Value=""3"" />" + Environment.NewLine;
            xaml += @"<SplineDoubleKeyFrame KeyTime=""00:00:3"" Value=""1"" />" + Environment.NewLine;
            xaml += @"</DoubleAnimationUsingKeyFrames>" + Environment.NewLine;
            xaml += @"<DoubleAnimationUsingKeyFrames BeginTime=""00:00:00"" Storyboard.TargetProperty=""(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)"">" + Environment.NewLine;
            xaml += @"<SplineDoubleKeyFrame KeyTime=""00:00:00"" Value=""1"" />" + Environment.NewLine;
            xaml += @"<SplineDoubleKeyFrame KeyTime=""00:00:1.5"" Value=""3"" />" + Environment.NewLine;
            xaml += @"<SplineDoubleKeyFrame KeyTime=""00:00:3"" Value=""1"" />" + Environment.NewLine;
            xaml += @"</DoubleAnimationUsingKeyFrames>" + Environment.NewLine;
            xaml += @"</Storyboard>" + Environment.NewLine;
            xaml += @"</Image.Resources>" + Environment.NewLine;
            xaml += @"<Image.Triggers>" + Environment.NewLine;
            xaml += @"<EventTrigger RoutedEvent=""FrameworkElement.Loaded"">" + Environment.NewLine;
            xaml += @"<BeginStoryboard Storyboard=""{StaticResource Loaded}"" />" + Environment.NewLine;
            xaml += @"</EventTrigger>" + Environment.NewLine;
            xaml += @"</Image.Triggers>" + Environment.NewLine;
            xaml += @"</Image>";

            return xaml;
        }

        #endregion

    }
}